package cn.itheima.web;

import cn.itheima.domain.Section;
import cn.itheima.domain.User;
import cn.itheima.service.SectionService;
import cn.itheima.service.SectionServiceImp;
import cn.itheima.service.UserService;
import cn.itheima.service.UserServiceImp;
import cn.itheima.utils.MD5Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: cuisensen
 * data: 2018/6/26.
 * time: 0:46.
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private UserService us=new UserServiceImp();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String txtName=null!=request.getParameter("txtName")?request.getParameter("txtName"):"";
        //String txtPwde= MD5Utils.md5(null!=request.getParameter("txtPwde")?request.getParameter("txtPwde"):"");
        String txtPwde= null!=request.getParameter("txtPwde")?request.getParameter("txtPwde"):"";
        String txtcode=null!=request.getParameter("txtcode")?request.getParameter("txtcode").toLowerCase():"";
        User user=us.login(txtName);
        HttpSession session=request.getSession();
        Map<String ,String> err=new HashMap<>();
        request.setAttribute("err",err);
        request.setAttribute("txtName",txtName);
        boolean flag=false;
        if("".equals(txtName)){
            err.put("txtName","请输入账号");
            flag=true;
        }
        if("".equals(txtPwde)){
            err.put("txtPwde","请输入密码");
            flag=true;
        }
        if(!session.getAttribute("randcode").equals(txtcode)){
            err.put("txtcode","验证码错误");
            flag=true;
        }
        if(null==user){
            err.put("txtName","用户名不存在");
            
            flag=true;
        }

        if(!txtPwde.equals(user.getUpassword())){
            err.put("txtPwde","密码错误");
            flag=true;
        }
        if(flag)
        	request.getRequestDispatcher(request.getContextPath()+"/login.jsp").forward(request,response);
        else {
        	Cookie username=new Cookie("username",txtName);
            Cookie password=new Cookie("password",txtPwde);
            password.setMaxAge(10*60);
            response.addCookie(username);
            response.addCookie(password);
            session.setAttribute("user",user);
            SectionService ss=new SectionServiceImp();
            List<Section> sections = ss.findSection(user.getCode());
            session.setAttribute("sections",sections);
            session.setAttribute("includePage","indexBody.jsp");
            response.sendRedirect(request.getContextPath()+"/index.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
